package com.archermind.boc.socket.server;

import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.filter.ssl.SslFilter;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.archermind.boc.Logger;
import com.archermind.boc.socket.BocsSslContextFactory;
import com.archermind.boc.socket.handler.BOCSServerHandler;
import com.archermind.boc.utils.UtilsTools;
import com.archermind.sessionpool.SessionPoolProtect;

public class BOCSServer {

	private static final int PORT = 5223;

	private final static Logger logger = Logger.getLogger(BOCSServer.class.getName());

	public static void main(String[] args) throws Exception {
		SocketAcceptor acceptor = new NioSocketAcceptor();
		acceptor.setReuseAddress(true);
		DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
		SslFilter sslFilter = new SslFilter(BocsSslContextFactory.getInstance(true));
		chain.addLast("sslFilter", sslFilter);
		acceptor.getFilterChain().addLast("Executor", new ExecutorFilter());
		// chain.addLast("codec", new ProtocolCodecFilter(new
		// TextLineCodecFactory()));
		acceptor.setHandler(new BOCSServerHandler());
		acceptor.bind(new InetSocketAddress(PORT));
		
		//start session pool
		SessionPoolProtect sessionPool=new SessionPoolProtect();
		sessionPool.checkSession(SessionPoolProtect.POLL_SECOND);
		logger.severe("[   Listen] [" + UtilsTools.getLocalIP() + "] [" + PORT + "]");
	}
}
